package com.libinyn.base.pms.serivce.impl.sys;


import com.libinyn.base.pms.common.exception.PmsException;
import com.libinyn.base.pms.common.utils.Constant;
import com.libinyn.base.pms.entity.sys.SysRoleEntity;
import com.libinyn.base.pms.mapper.sys.SysRoleMapper;
import com.libinyn.base.pms.serivce.impl.BaseServiceImpl;
import com.libinyn.base.pms.serivce.sys.SysRoleMenuService;
import com.libinyn.base.pms.serivce.sys.SysRoleService;
import com.libinyn.base.pms.serivce.sys.SysUserRoleService;
import com.libinyn.base.pms.serivce.sys.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色管理实现
 * @fileName: SysRoleServiceImpl.java
 * @author: bin.li01
 * @date: 2017/12/12 19:01 
 * @version: v1.0.0
 */
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleEntity> implements SysRoleService {

	@Resource
	private SysRoleMapper sysRoleMapper;
	@Resource
	private SysRoleMenuService sysRoleMenuService;
	@Resource
	private SysUserRoleService sysUserRoleService;
	@Resource
	private SysUserService sysUserService;

	@Override
	public void deleteBatch(Long[] roleIds) {
		sysRoleMapper.deleteBatch(roleIds);
	}
	
	@Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return sysRoleMapper.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(SysRoleEntity role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if(role.getCreateBy() == Constant.SUPER_ADMIN){
			return ;
		}
		
		//查询用户所拥有的菜单列表
		List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateBy());
		
		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new PmsException("新增角色的权限，已超出你的权限范围");
		}
	}
}
